//
//  YLMonthlyBillBrokenLineViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/10/27.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMonthlyBillBrokenLineViewModel.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "YLAccountTool.h"
#import "YLMonthlyBillBrokenLineModel.h"
@implementation YLMonthlyBillBrokenLineViewModel

- (instancetype)init
{
    if (self = [super init]) {
       // [self setUp];
    }
    return self;
}

- (RACCommand *)expectedReturnCommand
{
    if (!_expectedReturnCommand) {
        __weak typeof(self) weakSelf = self;
        _expectedReturnCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [[WHNetworkTool shareInstance] cancel];
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetCumulativeAmountByMonth", YLURL];
                
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                NSString *date = [dateFormatter stringFromDate:[NSDate date]];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:@{@"TimeIndex" : date} success:^(id responseObject) {
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        YLMonthlyBillBrokenLineItem *item = [YLMonthlyBillBrokenLineItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                        
                        [weakSelf.months removeAllObjects];
                        [weakSelf.investmentTotals removeAllObjects];
                        
                        for (YLMonthlyBillBrokenLineModel *model in item.item) {
                            [weakSelf.months addObject:[NSString stringWithFormat:@"%zd月",model.month]];
                            [weakSelf.investmentTotals addObject:@(model.amountTotal)];
                        }
                        
                        [subscriber sendNext:nil];
                        
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    
                    [subscriber sendCompleted];
                    
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                
                return nil;
            }];
        }];
    }
    return _expectedReturnCommand;
}

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [[WHNetworkTool shareInstance] cancel];
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetInvestmentByMonth", YLURL];
                
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                
                NSString *date = [dateFormatter stringFromDate:[NSDate date]];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:@{@"TimeIndex" : date} success:^(id responseObject) {
                    
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        YLMonthlyBillBrokenLineItem *item = [YLMonthlyBillBrokenLineItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                        [weakSelf.months removeAllObjects];
                        [weakSelf.investmentTotals removeAllObjects];
                        for (YLMonthlyBillBrokenLineModel *model in item.item) {
                            [weakSelf.months addObject:[NSString stringWithFormat:@"%zd月",model.month]];
                            [weakSelf.investmentTotals addObject:@(model.amountTotal / 10000.0)];
                        }
                        
                        [subscriber sendNext:nil];
                        
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    
                    [subscriber sendCompleted];
                    
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                
                return nil;
            }];
        }];
    }
    return _requestCommand;
}

- (NSMutableArray *)months
{
    if (!_months) {
        _months = [NSMutableArray arrayWithCapacity:5];
    }
    return _months;
}

- (NSMutableArray *)investmentTotals
{
    if (!_investmentTotals) {
        _investmentTotals = [NSMutableArray arrayWithCapacity:5];
    }
    return _investmentTotals;
}

- (void)dealloc
{
    [WHProgressHUD dismiss];
    [WHNetworkTool shareInstance];
}

@end
